/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package comparator;

import uk.ac.shef.wit.simmetrics.similaritymetrics.JaroWinkler;
import uk.ac.shef.wit.simmetrics.similaritymetrics.Levenshtein;
import uk.ac.shef.wit.simmetrics.similaritymetrics.OverlapCoefficient;

/**
 *
 * @author Patrik
 */
public class Comparator {
    
    private final JaroWinkler jaroAlg;
    private final Levenshtein levenAlg;
    private final OverlapCoefficient overLapAlg;
    
    public Comparator(){
        this.jaroAlg = new JaroWinkler();
        this.levenAlg = new Levenshtein();
        this.overLapAlg = new OverlapCoefficient();
    }
    
    public boolean match(String expected, String current) {
        String stringA = expected.replace("\\s\\s+?", " ").replace("\u00A0","").trim().toLowerCase();
        String stringB = current.replace("\\s\\s+?", " ").replace("\u00A0","").trim().toLowerCase();
         
        return (compareStringsJaro(stringA, stringB) >= 0.625 && compareStringsLeven(stringA, stringB) >= 0.625)
                || compareStringsOverLap(stringA, stringB) >= 0.70;
    }
     
    private double compareStringsJaro(String stringA, String stringB) {
        return jaroAlg.getSimilarity(stringA, stringB);
    }

    private double compareStringsLeven(String stringA, String stringB) {
        return levenAlg.getSimilarity(stringA, stringB);
    }

    private double compareStringsOverLap(String stringA, String stringB) {
        return overLapAlg.getSimilarity(stringA, stringB);
    }

    public double compareStrings(String stringA, String stringB) {
        String stringAA = stringA.replace("\\s\\s+?", " ").replace("\u00A0","").trim().toLowerCase();
        String stringBB = stringB.replace("\\s\\s+?", " ").replace("\u00A0","").trim().toLowerCase();
        
        double overLap = compareStringsOverLap(stringAA, stringBB);
        if (overLap > 0) {
            return overLap * compareStringsLeven(stringAA, stringBB) * compareStringsJaro(stringAA, stringBB);
        } else {
            return compareStringsLeven(stringAA, stringBB) * compareStringsJaro(stringAA, stringBB);
        }
    }
}
